/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import com.zc.BankCreditManagement.DAO.CustomerCreditLevelDAO;
import com.zc.BankCreditManagement.DAO.CustomerCreditLevelHistoryDAO;
import com.zc.BankCreditManagement.Exception.ZCException;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevel;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class CustomerCreditLevelServiceImpl {
	CustomerCreditLevelDAO cCLDAO;

	public CustomerCreditLevelServiceImpl() {
		this.cCLDAO = new CustomerCreditLevelDAO();
	}

	/**
	 * 更新当前信用登记表
	 * 
	 */
	public int updateCusCreditLevel(CustomerCreditLevel cusCL,boolean excludeDelete) {
		int count = 0;
		try {
			if (insertOldCusCLToHis(cusCL.getCusId(), excludeDelete) == 1) {// 将当前信用表插入到历史表中
				if (this.cCLDAO.deleteOldCusCreditLevelByIdFromDB(cusCL.getCusId(),excludeDelete) == 1) {// 删除当前信用标
					if (this.cCLDAO.insertNewCusCreditLevel(cusCL) == 1) {// 插入新数据
						count = 1;
					} else {
						throw new ZCException("插入新数据不成功！");
					}
				} else {
					throw new ZCException("删除不成功！");
				}
			} else {
				throw new ZCException("插入不成功！");
			}
		} catch (ZCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 将当前信用记录插入到历史表中 调用历史表的DAO将该记录插入历史表
	 */
	public int insertOldCusCLToHis(int cusId,boolean excludeDelete) {
		return new CustomerCreditLevelHistoryDAO().insertNewCusCreditHisLevel(this.getcCLById(cusId,excludeDelete));
	}

	/**
	 * 新建插入当前信用表
	 * 
	 */
	public int insertNewCusCL(CustomerCreditLevel cusCL) {
		return this.cCLDAO.insertNewCusCreditLevel(cusCL);
	}

	/**
	 * 获取该用户的当前信用表
	 * 
	 * @return
	 * 
	 *         null：没有该人的信用信息表
	 */
	public CustomerCreditLevel getcCLById(int cusId,boolean excludeDelete) {
		return this.cCLDAO.getCusCreditLevelByCusId(cusId,excludeDelete);
	}
}
